package com.hnhcyy.b2b.receiver.service;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.hnhcyy.b2b.customer.client.MchkClient;
import com.hnhcyy.b2b.customer.pojo.CustomerRebate;
import com.rabbitmq.client.Channel;

//@Component
public class OrderReceiver {
	
	@Autowired
    private MchkClient mchkClient;

	@RabbitListener(queues = "b2b.order.paid")
    public void process(Message message, Channel channel) throws Exception {
    	String str = new String(message.getBody(),"utf-8");
    	System.out.println("b2b.order.paid:" + str);
    	try {
    		JSONObject json = JSONObject.parseObject(str);
    		CustomerRebate customerRebate = mchkClient.rebate((String) json.get("sid"));
    		channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
		} catch (Exception e) {
			e.printStackTrace();
			//丢弃这条消息（绑定了死信队列，该消息会保存到死信队列）
			channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
		}
    }
    
}
